package com.leyou.item.web;

import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.SkuDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.dto.SpuDetailDTO;
import com.leyou.item.service.SkuService;
import com.leyou.item.service.SpuDetailService;
import com.leyou.item.service.SpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/goods")
public class GoodController {
    @Autowired
    private SkuService skuService;
    @Autowired
    private SpuService spuService;
    @Autowired
    private SpuDetailService spuDetailService;

    /**
     * 分页查询spu
     * @param page
     * @param rows
     * @param bid
     * @param cid
     * @param id
     * @param saleable
     * @return
     */
    @GetMapping("/spu/page")
    public ResponseEntity<PageDTO<SpuDTO>> getpage(
            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "rows",defaultValue = "2") Integer rows,
            @RequestParam(value = "brandId",required = false) Long bid,
            @RequestParam(value ="categoryId",required = false) Long cid,
            @RequestParam(value ="id",required = false) Long id,
            @RequestParam(value ="saleable",required = false) Boolean saleable
    ){
      return ResponseEntity.ok(spuService.getpage(page,rows,bid,cid,id,saleable));
    }

    /**
     * 根据id批量查询sku
     * @param ids
     * @return
     */
    @GetMapping("/sku/list")
    public ResponseEntity<List<SkuDTO>> queryGetList(@RequestParam("ids") List<Long> ids){
        return ResponseEntity.ok(skuService.queryGetList(ids));
    }

    /**
     * 根据SpuID在Sku表中查询集合数据
     * @param id
     * @return
     */
    @GetMapping("/sku/of/spu")
    public ResponseEntity<List<SkuDTO>> querySpuGetSkuId(@RequestParam("id") Long id){
        return ResponseEntity.ok(skuService.querySpuGetSkuId(id));

    }

    /**
     * 查询商品详情
     * @param id
     * @return
     */
    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailDTO> querySpuDetail(@RequestParam("id")  Long id){
        return ResponseEntity.ok(spuDetailService.querySpuDetail(id));
    }

    /**
     * 根据spuId以及是否可搜索查询规格参数集合信息
     * @param id
     * @param searching
     * @return
     */
    @GetMapping("/spec/value")
    public ResponseEntity<List<SpecParamDTO>> querySpecsValues(
            @RequestParam("id") Long id,
            @RequestParam(value = "searching",required = false) Boolean searching){

        return ResponseEntity.ok(spuService.querySpecsValues(id,searching));
       }

    /**
     * 根据商品id查询
      * @param id
     * @return
     */
    @GetMapping("/spu/{id}")
    public ResponseEntity<SpuDTO> querySpuGetById(@PathVariable("id") Long id){

        return ResponseEntity.ok(spuService.querySpuGetById(id));
    }
}
